﻿using GalaSoft.MvvmLight;
using ShoppingList.Model;
using System.Collections.Generic;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Collections.ObjectModel;
using ShoppingListEntities;
using System.Linq;
namespace ShoppingList.ViewModel
{
    
    public class MainViewModel : ViewModelBase
    {

        private ICommand tapCommand;
        
        public MainViewModel()
        {
            SetupCommands();
            CalculateValueItems();
            SetupTiles();
            //tileItems = new ObservableCollection<TileItem>()
            //{
            //    new TileItem() {
            //        ImageUri ="/Images/shopping.jpg", 
            //        Title = "compras", Notification = "600.43", GroupTag = "TileGroup",
            //        TapCommand= tapCommand
            //    },
            //    new TileItem() { 
            //        ImageUri ="/Images/products.jpg", 
            //        Title = "productos", Notification = "Only $2.49", GroupTag = "TileGroup" ,
            //        TapCommand= tapCommand
            //    }
				
            //};
        }
        #region Commands
       
        private void SetupCommands()
        {
            tapCommand = new RelayCommand<string>(HubTileTapAction);
        }
        #endregion
      
      

        #region Properties


        private ObservableCollection<ChartValueItem> _MyValueItems;
        public ObservableCollection<ChartValueItem> MyValueItems
        {
            get { return _MyValueItems; }

            set
            {
                _MyValueItems = value;
                RaisePropertyChanged("MyValueItems");
            }
        }


        private ObservableCollection<TileItem> _tileItems;
        public ObservableCollection<TileItem> tileItems
        {
            get { return _tileItems; }
            set
            {
                _tileItems = value;
                RaisePropertyChanged("tileItems");
            }
        }


        #region methods
        private void HubTileTapAction(string param)
        {
            if (param == "compras")
            {
                Messenger.Default.Send("shopping", "NavigationRequest");
            }

            if (param == "productos")
            {
                Messenger.Default.Send("products", "NavigationRequest");
            }
            if (param == "estadisticas")
            {
                Messenger.Default.Send("statistics", "NavigationRequest");
            }
            
        }

        private void CalculateValueItems()
        {

            MyValueItems = new ObservableCollection<ChartValueItem>();

            MyValueItems.Add(new ChartValueItem { StringDate="26/11/2011", result=300 });
            MyValueItems.Add(new ChartValueItem { StringDate = "27/11/2011", result = 600 });
            MyValueItems.Add(new ChartValueItem { StringDate = "29/11/2011", result = 345.4 });
            MyValueItems.Add(new ChartValueItem { StringDate = "30/11/2011", result = 345.4 });
            MyValueItems.Add(new ChartValueItem { StringDate = "1/12/2011", result = 100 });
            MyValueItems.Add(new ChartValueItem { StringDate = "2/12/2011", result = 1200 });
            MyValueItems.Add(new ChartValueItem { StringDate = "3/12/2011", result = 96.5 });

        }

        public void SetupTiles()
        {
            tileItems = new ObservableCollection<TileItem>();

            TileItem ShoppingTile = new TileItem();
                    ShoppingTile.ImageUri ="/Images/shopping.jpg"; 
				    ShoppingTile.Title = "compras";
                   
                    string CostOfDay= string.Format("Costo acumulado el dia de hoy! : ${0}",getCostOfDay());
                    ShoppingTile.Message = CostOfDay;       
                    //ShoppingTile.Notification = CostOfDay;
                    ShoppingTile.GroupTag = "TileGroup";

                    ShoppingTile.TapCommand= tapCommand;

                    TileItem ProductTile = new TileItem();
                    ProductTile.ImageUri = "/Images/products.jpg";
                    ProductTile.Title = "productos";

                    string count = string.Format("Total de productos dados de alta : {0}", getTotalProducts());
                    ProductTile.Message = count;
                   // ProductTile.Notification = count;
                    ProductTile.GroupTag = "TileGroup";

                    ProductTile.TapCommand = tapCommand;

                    tileItems.Add(ShoppingTile);
                    tileItems.Add(ProductTile);
        }

        private string getCostOfDay()
        {
            double Total;
            ShoppingListEntities.ShoppingList currentShoppingList = Helpers.ShopListHelpers.getShoppingList();
            using (ShoppingListDataContext dataContext = new ShoppingListDataContext(Helpers.DataBaseHelpers.Cnn))
            {
                //        dataContext.DeleteDatabase();
                if (dataContext.DatabaseExists())
                {
                    if (currentShoppingList != null) { 
                    var accounts = dataContext.ShoppingLists;

                    List<ShoppingListEntities.ShoppingList> list = accounts.ToList();

                    var query = list.Where(x => x.ShoppingListId == currentShoppingList.ShoppingListId);
                    Total = 0.00;

                    foreach (var item in query)
                    {

                        double count = 0.00;



                        var shoppingproducts = dataContext.ShoppingListProducts
                            .Where(x => x.ShoppingListId == item.ShoppingListId);

                        foreach (var shop in shoppingproducts)
                        {


                            var elements = dataContext.ProductElements.Where(x => x.ProductId == shop.ProductId && x.ShoppingListId == shop.ShoppingListId);


                            foreach (var element in elements)
                            {
                                count += element.Cost;
                            }

                        }

                        Total = count;
                        return Total.ToString();
                    }
                    }


                }
            }
            return string.Format("0");
          

        }

        private string getTotalProducts()
        {
            return Helpers.DataBaseHelpers.getTotalOfProducts().ToString();
        }
        #endregion
        #endregion
    }
}